package com.azusa.xbiao.service.impl;

import com.azusa.xbiao.dao.UserDao;
import com.azusa.xbiao.dao.UserMapper;
import com.azusa.xbiao.entity.Goods;
import com.azusa.xbiao.entity.User;
import com.azusa.xbiao.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private UserMapper userMapper;

    @Override
    public User save(User loginUser) {
        return userDao.save(loginUser);
    }

    @Override
    public User findByUsernameAndPassword(String username, String password) {
        return userDao.findByUsernameAndPassword(username, password);
    }

    @Override
    public User findUserLookById(Integer userId) {
        User user = userDao.findById(userId).get();
        user.setOrderCount(userMapper.selectUserOrderCount(userId));
        user.setCartCount(userMapper.selectUserCartCount(user.getcId()));
        return user;
    }

    @Override
    public User findByEmail(String email) {
        return userDao.findByEmail(email);
    }

    @Override
    public User findByUsername(String username) {
        return userDao.findByUsername(username);
    }

    @Override
    public User findByUsernameAndEmail(String username, String email) {
        return userDao.findByUsernameAndEmail(username, email);
    }

    @Override
    public PageInfo<Goods> findCollect(int pageNum,int pageSize,int userId) {
        List<Integer> ids = userMapper.selectCollect(userId);
        PageHelper.startPage(pageNum, pageSize);
        List<Goods> goods = userMapper.selectCollectGoods(ids);
        return new PageInfo<>(goods);
    }

    @Override
    public User findByWxOpenid(String openid) {
        return userDao.findByWxOpenid(openid);
    }
}
